package com.qububu.play.dineTogether.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.play.dineTogether.dto.ActionPeopleDto;
import com.qububu.play.dineTogether.service.DinePeopleService;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

@Controller
@RequestMapping("/play/dine")
public class DinePeopleControl {

	@Resource
	private DinePeopleService dinePeopleService;
	
	/**
	 * @功能 根据actionId查询foodList
	 */
	@RequestMapping("/getPeoples")
	@ResponseBody
	public Map<String, Object> getActionPeopleList(HttpServletRequest request, HttpServletResponse response){
		String actionId = SystemUtil.decrypt(ParamConverter.assertNotEmpty(request.getParameter("actionId")));
		int reActionId = ParamConverter.convertToInteger(actionId);
		List<ActionPeopleDto> peopleList = dinePeopleService.getActionPeopleListByActionId(reActionId);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("peoples", peopleList);
		
		return resultMap;
	}
	
	
	/**
	 * @功能 根据peopleId查看People信息，此处和user里边查看信息都是一样的，只不过传入的参数不一样
	 */
	
	
	/**
	 * @功能 添加peopleList
	 */
	@RequestMapping("/ajaxAddActionPeople")
	@ResponseBody
	public Map<String, Object> addActionPeople(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> actionPeopleMap = new HashMap<String, Object>();

		if (request.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME) == null) {
			actionPeopleMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			actionPeopleMap.put("message", "请先登录");
		}
		UserInfoDto userInfoDto = (UserInfoDto) request.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
		String peopleName = userInfoDto.getNickName();
		String peopleId = userInfoDto.getUserId();

		String staffId = SystemUtil.encrypt("0");
		String actionId = assertNotEmpty(request.getParameter("peopleActionId"));
		
		ActionPeopleDto actionPeopleDto = new ActionPeopleDto(peopleId, staffId, actionId);
		actionPeopleDto.setPeopleName(peopleName);
		
		// 先要看看数据库中有没得这个对象，如果有就能再添加了并且提示不能再添加
		if(dinePeopleService.doAddActionPeople(actionPeopleDto)) {
			actionPeopleMap.put("result", SystemConstants.AJAX_SUCCESS);	
		} else {
			actionPeopleMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			actionPeopleMap.put("message", "您已经参加了该活动");
		}
		
		return actionPeopleMap;
	}
	
	/**
	 * @功能 删除
	 */
	@RequestMapping("/admin/ajaxDeletePeople")
	@ResponseBody
	public Map<String, Object> doDeleteActionPeopleByActionId(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> deletePeopleMap = new HashMap<String, Object>();
		
		int peopleId = ParamConverter.convertToInteger(SystemUtil.decrypt(assertNotEmpty(request.getParameter("peopleId"))));
		
		if (dinePeopleService.doDeleteActionPeopleByActionId(peopleId)) {
			deletePeopleMap.put("result", SystemConstants.AJAX_SUCCESS);	
		} else {
			deletePeopleMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			deletePeopleMap.put("result", "缓存操作失败,请稍后重试");
		}
		
		return deletePeopleMap;
	}
	
	/**
	 * 指定people的职务ID
	 * */
	@RequestMapping("/admin/changePeopleStaff")
	@ResponseBody
	public Map<String, Object> changePeopleStaff(HttpServletRequest req) {
		
		int peopleId = ParamConverter.convertToInteger(SystemUtil.decrypt(assertNotEmpty(req.getParameter("peopleId"))));
		int staffId = ParamConverter.convertToInteger(SystemUtil.decrypt(assertNotEmpty(req.getParameter("staffId"))));
		dinePeopleService.doChangePeopleStaff(peopleId, staffId);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		
		return resultMap;
	}
	
	/**
	 * @功能 修改
	 * @备注 People里边只有peopleName以及几个id，都是不可以更改的信息，暂时不考虑
	 */
	
}
